<?php
namespace app\index\domain;

class Content extends Base
{
    private $_id;
    private $_self = [];
    public function __construct($id)
    {
        if(is_array($id)){
            $this->_self = $self = $id;
            $this->_id = $self['id'];
        }else{
            $this->_id = $id;
            //$this->_self = D('index/Content')->getInfo($id);
        }
    }

    public function valueGet($key)
    {

        $result=$this->getself($key);

        if(isset($result))
            return $result;
        return null;
    }

    /**
     * 所属栏目
     */
    public function getTerm()
    {
        return Classes::get('Term', $this->getSelf('term_id'));
    }

    /**
     * 获取内容数据
     */
    public function getSelf($field)
    {
        if(isset($this->_self[$field]))
            return $this->_self[$field];
        $this->_self[$field] = D('index/content')->getInfo($this->_id, $field);
        return $this->_self[$field];
        //if(!isset(D('index/content')->getInfo($this->_id)[$field]))
            //return null;
    }

    /**
     * 获取当前内容页的标题
     */
    public function getTitle()
    {
        if(isset($this->_title))
            return $this->_title;
        $option = Classes::get('Option');
        if(!empty($this['seo_title']))
            return $this->_title = $this['seo_title'];
        if(isset($option['title']))
            return $this->_title = $this['name'].'-'.$option['title'];
        else
            return $this->_title = $this['name'];
    }

    /**
     * 获取上一篇
     */
    public function getLast()
    {
        $last = D('index/Content')->getLast($this->_id);
        if($last)
            return Classes::get('Content', $last);
        return [
            'name'=>'暂无内容',
            'url'=>'#',
        ];
    }

    /**
     * 获取下一篇
     */
    public function getNext()
    {
        $next = D('index/Content')->getNext($this->_id);
        if($next)
            return Classes::get('Content', $next);
        return [
            'name'=>'暂无内容',
            'url'=>'#',
        ];
    }

    /**
     * 获取此内容页的地址
     */
    public function getUrl()
    {
        $url = U('/c/'.$this['id']);
        return $url;
    }

    /**
     * 获取此内容页的地址
     */
    public function getTag()
    {
        $tag = json_decode($this['tags_value'], true);
        return $tag;
    }
}

